package com.qianmang.damawangticket.activity;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.qianmang.damawangticket.utils.KeyBoardUtils;


/**
 * Author 钱漭
 * Date 2019/11/13
 * QQ 527695148
 * Mail 527695148@qq.com
 */
public abstract class BaseActivity extends AppCompatActivity {


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(loadLayoutRes());
        onFindView();
        onInitSet();
    }

    /**
     * 获取布局文件id
     *
     * @return
     */
    @LayoutRes
    protected abstract int loadLayoutRes();

    /**
     * 获取该布局文件的空间对象
     */
    protected abstract void onFindView();

    /**
     * 对控件进行逻辑处理
     */
    protected void onInitSet() {
    }

    protected void goActivity(Class<? extends BaseActivity> activityClass) {
        Intent intent = new Intent(this, activityClass);
        startActivity(intent);
    }

    protected void goActivity(Class<? extends Activity> activityClass, Bundle bundle) {
        Intent intent = new Intent(this, activityClass);
        intent.putExtras(bundle);
        startActivity(intent);
    }


    protected void goActivityAndFinish(Class<? extends Activity> activityClass) {
        Intent intent = new Intent(this, activityClass);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    /**
     * 当用户触摸非输入控件时隐藏键盘
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (v != null && isFocusEditText(v, hideSoftByEditViewIds())) {
                KeyBoardUtils.hideKeyBoard(this, v);
                v.clearFocus();
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    private boolean isFocusEditText(View v, int[] editTextIds) {
        if (editTextIds == null) return false;

        for (int i : editTextIds) {
            if (v.getId() == i) {
                return true;
            }
        }


        return false;
    }

    /**
     * @return 存储{@link android.widget.EditText}的ID
     */
    @Nullable
    protected int[] hideSoftByEditViewIds() {
        return null;
    }

}
